package com.tywj.buscustomerapp.view.myview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.daimajia.numberprogressbar.NumberProgressBar;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.utils.PhoneUtils;

public class TaylorPopupWindowD extends PopupWindow {

    private Activity context;
    private View contentView;
    private LatLng start;
    private TextView gd;
    private TextView bd;
    private TextView tx;
    public TaylorPopupWindowD(Context context, LatLng start) {
        super(context);
        this.context = (Activity) context;
        this.start = start;
        this.initPopupWindow();
    }
    public TaylorPopupWindowD(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TaylorPopupWindowD(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private void initPopupWindow() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.popuwindow_layoutd, null);
        this.setContentView(contentView);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.whitegray));
        this.setBackgroundDrawable(dw);
        this.setContentView(contentView);
        this.setAnimationStyle(android.R.style.Animation_Dialog);
        gd = contentView.findViewById(R.id.to_gd);
        bd = contentView.findViewById(R.id.to_bd);
        tx = contentView.findViewById(R.id.to_tx);
        gd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!PhoneUtils.isInstalled("com.autonavi.minimap",context)) {
                    ToastJoe.getmToastJoe().ToastLongShow(context,null,"检测到您未安装高德地图");
                    return;
                }
                StringBuffer stringBuffer = new StringBuffer("amapuri://route/plan/?sourceApplication=").append("胖哒直通车");
                stringBuffer.append("&dlat=").append(start.latitude)
                        .append("&dlon=").append(start.longitude)
                        .append("&dev=").append(0)
                        .append("&t=").append(2);
                Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(stringBuffer.toString()));
                intent.setPackage("com.autonavi.minimap");
                context.startActivity(intent);
            }
        });
        bd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!PhoneUtils.isInstalled("com.baidu.baidumap",context)) {
                    ToastJoe.getmToastJoe().ToastLongShow(context,null,"检测到您未安装百度地图");
                    return;
                }
                StringBuffer stringBuffer = new StringBuffer("baidumap://map/direction?");
                stringBuffer.append("destination=").append(start.latitude)
                        .append(",").append(start.longitude)
                        .append("&coord_type=").append("gcj02")
                        .append("&mode=").append("walking");
                Intent i1 = new Intent();
                i1.setData(Uri.parse(stringBuffer.toString()));
                context.startActivity(i1);
            }
        });
        tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!PhoneUtils.isInstalled("com.tencent.map",context)) {
                    ToastJoe.getmToastJoe().ToastLongShow(context,null,"检测到您未安装腾讯地图");
                    return;
                }
                StringBuffer stringBuffer = new StringBuffer("qqmap://map/routeplan?");
                stringBuffer.append("type=").append("walk")
                        .append("&tocoord=").append(start.latitude).append(",").append(start.longitude)
                        .append("&referer=").append("LQDBZ-GOEKP-I7IDR-LZ3V6-6YRCH-2NF2Z");
                Intent i1 = new Intent();
                i1.setData(Uri.parse(stringBuffer.toString()));
                context.startActivity(i1);
            }
        });
    }


    public void showAtDropDownCenter(View parent) {
        if (parent.getVisibility() == View.GONE) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        } else {
            this.showAtLocation(parent, Gravity.BOTTOM, 0,0);
        }
    }




    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(anchor);
    }

}
